/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.meraklisina.latexeditor.mdi.TextEditor;



import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;

import com.meraklisina.latexeditor.mdi.Common.Tools;
import com.meraklisina.latexeditor.mdi.DialogActions.ActionSave;

/**
 *
 * @author Eray
 */
public class Actions
{
    TextEditors ClsTextEditor = new TextEditors();
    ActionSave ClsActionSave = new ActionSave();

    
    /**
     * Saving the changed tabs into files with prompt.
     *
     * @param TabEditors
     * @param StartupPath
     */
    public void SaveQuiet(JTabbedPane TabEditors, String StartupPath)
    {
        int Index = 0;


        for(TextEditors editor : ClsTextEditor.getEditors())
        {

            if(editor.getScrapPath().isEmpty() && editor.getIsTextChanged()) //This is for NEW SCRAP files
            {
                int DialogResult = JOptionPane.showConfirmDialog(
                                                         null,
                                                         "Do you want to save the changes on file '" + TabEditors.getTitleAt(Index).replace(" *", "") + "' first?",
                                                         "Save Before Exit",
                                                         JOptionPane.YES_NO_OPTION);
                if(DialogResult == JOptionPane.YES_OPTION)
                {
                    ClsActionSave.SaveDialog(TabEditors, "Save", false);
                }
                else
                {
                    if(Tools.isSaveInScrapFiles())
                    {
                        int ScrapFileNumber = ClsTextEditor.GenerateScrapFileNumber();
                        ClsActionSave.Save(TabEditors, StartupPath + Tools.getScrapFilesPath() + TextEditors.getName_ScrapFile() + ScrapFileNumber + ".scrap", true);

                        editor.setIsTextChanged(true);
                        editor.setScrapPath(Tools.getScrapFilesPath() + TextEditors.getName_ScrapFile() + String.valueOf(ScrapFileNumber) + ".scrap");
                    }

                }
            }
            else if(editor.getIsTextChanged()) //This is for EXISTING SCRAP files
            {
                int DialogResult = JOptionPane.showConfirmDialog(
                                                         null,
                                                         "Do you want to save the changes on file '" + TabEditors.getTitleAt(Index).replace(" *", "") + "' first?",
                                                         "Save Before Exit",
                                                         JOptionPane.YES_NO_OPTION);
                if(DialogResult == JOptionPane.YES_OPTION)
                {
                    ClsActionSave.SaveDialog(TabEditors, "Save", false);
                }
                else
                {
                    if(Tools.isSaveInScrapFiles())
                    {
                        ClsActionSave.Save(TabEditors, StartupPath + editor.getScrapPath(), true);
                        editor.setIsTextChanged(true);
                    }
                }
            }

            Index++;
        }
    }
    /**
     *
     * This function is usually for before building the file. Saves into a
     * temporary scrap file at a selected TextEditor Collection index.
     *
     * @param TabEditors
     * @param StartupPath
     * @return Path of the saved ScrapFile
     */
    public String SaveQuietAt(JTabbedPane TabEditors, String StartupPath)
    {
        int Index = TabEditors.getSelectedIndex();

        if(ClsTextEditor.GetScrapPath(Index).isEmpty() && ClsTextEditor.GetIsTextChanged(Index)) //This is for NEW SCRAP files
        {
            int ScrapFileNumber = ClsTextEditor.GenerateScrapFileNumber();
            ClsActionSave.Save(TabEditors, StartupPath + Tools.getScrapFilesPath() + TextEditors.getName_ScrapFile() + ScrapFileNumber + ".scrap", true);

            ClsTextEditor.SetIsTextChange(TabEditors, true);
            ClsTextEditor.SetScrapPath(Index, Tools.getScrapFilesPath() + TextEditors.getName_ScrapFile() + String.valueOf(ScrapFileNumber) + ".scrap");
        }
        else if(ClsTextEditor.GetIsTextChanged(Index)) //This is for EXISTING SCRAP files
        {
            ClsActionSave.Save(TabEditors, StartupPath + ClsTextEditor.GetScrapPath(Index), true);
            //Save(Form, ClsTextEditor.GetScrapPath(Index), Index, true);
            ClsTextEditor.SetIsTextChange(TabEditors, true);
        }


        return ClsTextEditor.GetScrapPath(Index);
    }
}
